﻿package com.jfdesgagne.jfdesgagne09.section.showcase.controller {
	import com.jfdesgagne.jfdesgagne09.section.showcase.view.ShowcaseView;
	import com.jfdesgagne.jfdesgagne09.section.showcase.model.ShowcaseModel;
	import com.jfdesgagne.jfdesgagne09.section.showcase.event.ProjectsLoadedEvent;
	
	import com.jfdesgagne.jfdesgagne09.JFDesgagne09;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class ShowcaseController extends EventDispatcher {
		
		private var _showcaseView:ShowcaseView;
		private var _showcaseModel:ShowcaseModel;
		private var _jfDesgagne:JFDesgagne09;
		private var _ready:Boolean;
		
		public function ShowcaseController($jfDesgagne:JFDesgagne09) {
			this._jfDesgagne = $jfDesgagne;
			this._ready = false;
			this._initModel();
			
		}
		
		private function _initModel():void {
			this._showcaseModel = new ShowcaseModel(this._jfDesgagne.gateway);
			this._showcaseModel.addEventListener(ProjectsLoadedEvent.COMPLETE, this._projectsLoadedHandler);
			this._showcaseModel.loadProjects();
		}
		
		private function _projectsLoadedHandler($event:ProjectsLoadedEvent):void {
			if(this._ready) this._init();
		}
		
		private function _init():void {
			trace("showcase loaded:"+this._showcaseModel.projects);
			this._ready = true;
			this._initView();
			this._initEvents();
		}
		
		private function _initView():void {
			this._showcaseView = new ShowcaseView();
			this._jfDesgagne.addChild(this._showcaseView);
			this._showcaseView.init();
		}
		
		private function _initEvents():void {
			
		}
		
		public function show():void {
			if(this._showcaseModel.projects==null) {
				this._ready = true;
			} else {
				this._init();
			}
		}		
		
		public function unload():void {
			this.dispatchEvent(new Event(Event.UNLOAD));
		}
	}
}